﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;
using AutoMapper;

namespace KernelWorkflow.Activities
{
    public partial class GetExamsByStudentActivity : SequenceActivity
    {
        private bool crutch_first_time = true;
        //in
        public static DependencyProperty CurrentSessionProperty = DependencyProperty.Register("CurrentSession", typeof(SessionDto), typeof(GetExamsByStudentActivity));
        public static DependencyProperty CurrentStudentGuidProperty = DependencyProperty.Register("CurrentStudentGuid", typeof(Guid), typeof(GetExamsByStudentActivity));
        //out
        public static DependencyProperty ExamsByStudentProperty = DependencyProperty.Register("ExamsByStudent", typeof(ExamsListFullDataDto), typeof(GetExamsByStudentActivity));

        [Category("AUTHORIZATION_INPUT")]
        [Browsable(true)]
        public SessionDto CurrentSession
        {
            get
            {
                return ((SessionDto)(base.GetValue(GetExamsByStudentActivity.CurrentSessionProperty)));
            }
            set
            {
                base.SetValue(GetExamsByStudentActivity.CurrentSessionProperty, value);
            }
        }

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public Guid CurrentStudentGuid
        {
            get
            {
                return ((Guid)(base.GetValue(GetExamsByStudentActivity.CurrentStudentGuidProperty)));
            }
            set
            {
                base.SetValue(GetExamsByStudentActivity.CurrentStudentGuidProperty, value);
            }
        }

        [Category("DATA_OUTPUT")]
        [Browsable(true)]
        public ExamsListFullDataDto ExamsByStudent
        {
            get
            {
                return ((ExamsListFullDataDto)(base.GetValue(GetExamsByStudentActivity.ExamsByStudentProperty)));
            }
            set
            {
                base.SetValue(GetExamsByStudentActivity.ExamsByStudentProperty, value);
            }
        }


        //----------------------------------------------------------------------------------------------
        public static readonly string GetExamsByStudentActivityAccessObject = "GetExamsByStudent";
        public Boolean isSessionOk = false;
        public ExamListDto ExamList;
        public StudentDto CurrentStudentDto;
        public SubjectDto CurrentSubjectDto;
        public TeacherDto CurrentTeacherDto;
        public Guid CurrentSubjectGuid;
        public Guid CurrentTeacherGuid;


        public List<ExamWithSubjectAndTeacherDto> EWSTLIST;
        public ExamWithSubjectAndTeacherDto EWST;
        public IEnumerator<ExamDto> ExamListEnumerator;


        private void CheckRightsAfterResponseHook(object sender, SendActivityEventArgs e)
        {
            Console.WriteLine("GetExamsByStudentActivity::isSessionOk = {0}", this.isSessionOk);
        }

        private void MakeResultExamsFullDataListDto(object sender, SendActivityEventArgs e)
        {
            if (crutch_first_time)
            {
                Mapper.CreateMap<ExamDto, ExamWithSubjectAndTeacherDto>();
                crutch_first_time = false;
            }

            this.ExamsByStudent = new ExamsListFullDataDto();
            this.EWSTLIST = new List<ExamWithSubjectAndTeacherDto>();
            this.ExamsByStudent.Exams = this.EWSTLIST;

            Console.WriteLine("MakeResultExamsFullDataListDto");
        }
        private void GetExamListEnumerator(object sender, SendActivityEventArgs e)
        {
            this.ExamListEnumerator = this.ExamList.Exams.GetEnumerator();
            this.CurrentStudentGuid = this.ExamList.StudentGuid;

            Console.WriteLine("GetExamListEnumerator");
        }
        private void AssignCurrnetStudentDto(object sender, SendActivityEventArgs e)
        {
            this.ExamsByStudent.Student = this.CurrentStudentDto;

            Console.WriteLine("AssignCurrentStudentDto");
        }
        private void GetCurrentSubjectGuid(object sender, SendActivityEventArgs e)
        {
            this.CurrentSubjectGuid = this.ExamListEnumerator.Current.SubjectGuid;

            Console.WriteLine("GetCurrentSubjectGuid");
        }
        private void GetCurrentTeacherGuid(object sender, SendActivityEventArgs e)
        {
            this.CurrentTeacherGuid = this.ExamListEnumerator.Current.TeacherGuid;

            Console.WriteLine("GetCurrentTeacherGuid");
        }
        private void StoreExamWithSubjectAndTeacher(object sender, SendActivityEventArgs e)
        {
            this.EWST = Mapper.Map<ExamDto, ExamWithSubjectAndTeacherDto>(this.ExamListEnumerator.Current);
            this.EWST.Subject = this.CurrentSubjectDto;
            this.EWST.Teacher = this.CurrentTeacherDto;
            this.EWSTLIST.Add(this.EWST);

            Console.WriteLine("StoreExamWithSubjectAndTeacher");
        }

        public GetExamsByStudentActivity()
        {
            InitializeComponent();
        }
    }
}
